package api

import (
	"github.com/gin-gonic/gin"
	"todolist/pkg/e"
	"todolist/pkg/utils"
	"todolist/service"
)

// @Router /task [post]
func CreateTask(c *gin.Context) {
	createService := service.CreateTaskService{}
	chaim, _ := utils.ParseToken(c.GetHeader("Authorization"))
	if err := c.ShouldBind(&createService); err == nil {
		res := createService.Create(chaim.ID)
		c.JSON(e.SUCCESS, res)
	} else {
		c.JSON(e.ERROR, ErrorResponse(err))
		utils.LogrusObj.Info(err)
	}
}

// @Router /tasks [get]
func ListTasks(c *gin.Context) {
	listService := service.ListTasksService{}
	chaim, _ := utils.ParseToken(c.GetHeader("Authorization"))
	if err := c.ShouldBind(&listService); err == nil {
		res := listService.List(chaim.ID)
		c.JSON(e.SUCCESS, res)
	} else {
		c.JSON(e.ERROR, ErrorResponse(err))
		utils.LogrusObj.Info(err)
	}
}

// @Router /task/:id [get]
func ShowTask(c *gin.Context) {
	showTaskSerice := service.ShowTaskService{}
	chaim, _ := utils.ParseToken(c.GetHeader("Authorization"))
	res := showTaskSerice.Show(c.Param("id"), chaim.ID)
	c.JSON(e.SUCCESS, res)
}

// @Router /task/:id [delete]
func DeleteTask(c *gin.Context) {
	deleteTaskService := service.DeleteTaskService{}
	chaim, _ := utils.ParseToken(c.GetHeader("Authorization"))
	res := deleteTaskService.Delete(c.Param("id"), chaim.ID)
	c.JSON(e.SUCCESS, res)
}

// @Router /task [put]
func UpdateTask(c *gin.Context) {
	updateTaskService := service.UpdateTaskService{}
	if err := c.ShouldBind(&updateTaskService); err == nil {
		chaim, _ := utils.ParseToken(c.GetHeader("Authorization"))
		res := updateTaskService.Update(c.Param("id"), chaim.ID)
		c.JSON(e.SUCCESS, res)
	} else {
		c.JSON(e.ERROR, ErrorResponse(err))
		utils.LogrusObj.Info(err)
	}
}

// @Router /search [post]
func SearchTasks(c *gin.Context) {
	searchTaskService := service.SearchTaskService{}
	chaim, _ := utils.ParseToken(c.GetHeader("Authorization"))
	if err := c.ShouldBind(&searchTaskService); err == nil {
		res := searchTaskService.Search(chaim.ID)
		c.JSON(e.SUCCESS, res)
	} else {
		c.JSON(e.ERROR, ErrorResponse(err))
		utils.LogrusObj.Info(err)
	}
}
